package com.just.starterhello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration         //开启配置
@EnableConfigurationProperties(HelloProrperties.class) //开启使用映射实体对象
@ConditionalOnClass(HelloService.class) //存在HelloService类时初始化配置类
@ConditionalOnProperty(   //存在对应配置信息时初始化该配置类
        prefix = "hello",
        value = "enable",
        matchIfMissing = true //缺失检查
)

public class HelloAutoConfiguration {
    @Autowired
    private HelloProrperties helloProrperties;

    @Bean
    @ConditionalOnMissingBean(HelloService.class) //缺失HelloService实体Bean时，创建HelloService Bean并加入到SpringIOC中
    HelloService helloService(){
        System.out.println(">>>The HelloService Not Found，Execute Create New Bean.");
        HelloService helloService=new HelloService();
        helloService.setMsg(helloProrperties.getMsg());
        return helloService;
    }
}
